﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEditor;
using System.IO;

namespace Core
{
    [Serializable]
    public class AssetBundleBuildConfig
    {
        [Serializable]
        public class BuildConfig
        {
            public string localVersionPath;
            public string localBuildPath;
            public string serverVersionUrl;
        }

        public Dictionary<string, BuildConfig> buildConfig = new Dictionary<string, BuildConfig>();

        private static AssetBundleBuildConfig ins;
        public static AssetBundleBuildConfig Ins
        {
            get 
            { 
                if (ins == null)
                {
                    ins = Load();
                }
                return ins; 
            }
        }

        public static AssetBundleBuildConfig Load()
        {
            var savePath = GetSavePath();
            if (File.Exists(savePath))
            {
                var content = File.ReadAllText(savePath);
                return Utils.JsonToObject<AssetBundleBuildConfig>(content);
            }

            var config = new AssetBundleBuildConfig();
            return config;
        }

        public static void Reload()
        {
            var savePath = GetSavePath();
            if (File.Exists(savePath))
            {
                var content = File.ReadAllText(savePath);
                ins = Utils.JsonToObject<AssetBundleBuildConfig>(content);
            }
        }

        public BuildConfig GetBuildConfig(BuildTarget buildTarget)
        {
            var key = buildTarget.ToString();
            if (!buildConfig.TryGetValue(key, out var config))
            {
                config = new BuildConfig();
                buildConfig[key] = config;
            }
            return config;
        }

        public void Save()
        {
            var savePath = GetSavePath();
            var content = Utils.ObjectToJson(this);
            File.WriteAllText(savePath, content);
        }

        public static string GetSavePath()
        {
            return $"{Application.dataPath}/../AssetBundleBuildConfig.json";
        }
    }
}



